<% this.title = '重设密码';  %>
<% include header %>
<div class="ui message" id="msgBox" hidden>
    <p id="msgContent"></p>
</div>
<div class="ui middle aligned center aligned grid">
  <div class="row">
    <div class="column" style="max-width: 450px">
      <h2 class="ui image header">
        <div class="content">
          重设密码
        </div>
      </h2>
      <form class="ui large form" id="resetForm">
        <div class="ui existing segment">
            <div class="field">
                <div class="ui left icon input">
                    <i class="lock icon"></i>
                    <input type="password" placeholder="密码" id="password1">
                </div>
            </div>
            <div class="field">
                <div class="ui left icon input">
                    <i class="undo icon"></i>
                    <input type="password" placeholder="确认密码" id="password2">
                </div>
            </div>
            <button type="submit" class="ui fluid large submit button" id="resetPassword">重设</div>
        </div>
      </form>
    </div>
  </div>
</div>
<script src="<%- lib('blueimp-md5/2.10.0/js/md5.min.js') %>"></script>
<script type="text/javascript">
function showMessage(mclass, content) {
    $("#msgBox").addClass(mclass);
    $("#msgContent").text(content);
    $("#msgBox").show();
}
function submitForm() {
    if ($("#password1").val() != $("#password2").val()) {
        showMessage("两次输入的密码不一致");
        return;
    }
    password = md5($("#password1").val() + "syzoj2_xxx");
    $("#resetPassword").addClass("loading");
    $.ajax({
        url: "/api/reset_password",
        type: 'POST',
        data: {
            "token": <%- serializejs(token) %>,
            "password": password
        },
        async: true,
        success: function(data) {
            error_code = data.error_code;
            switch (error_code) {
                case 1:
                    showMessage("positive", "密码重置成功。");
                    break;
                default:
                    showMessage("error", "未知错误 " + error_code);
                    break;
            }
            $("#resetPassword").removeClass("loading");
        },
        error:  function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.responseText);
        }
    });
}
$(document).ready(function() {
    $("#resetForm").submit(function(event) {
        submitForm();
        event.preventDefault();
    });
});
</script>
